package jmu.cultural_tourism.controller;

import io.swagger.v3.oas.annotations.Operation;
import jmu.cultural_tourism.entity.CityInfo;
import jmu.cultural_tourism.service.CityInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/*城市文旅表(CityInfo)表控制层*/
@RestController
@RequestMapping("cityInfo")
public class CityInfoController {
    /*服务对象*/
    @Autowired
    private CityInfoService cityInfoService;

    /*分页查询（带条件）*/
    @Operation(summary = "分页展示城市文旅信息")
    @PostMapping({"city/{pageNo}/{pageSize}","city/{pageNo}","city"})
    public ResponseEntity<Page<CityInfo>> queryByPage(
            @RequestBody CityInfo cityInfo,
            @PathVariable(value = "pageNo",required = false) Integer pageNo,
            @PathVariable(value = "pageSize",required = false) Integer pageSize)
    {
        if (pageNo == null) {
            pageNo = 0; // 第一页
        } else {

            pageNo = pageNo - 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        return ResponseEntity.ok(this.cityInfoService.queryByPage(cityInfo, pageRequest));
    }

    /*分页查询所有城市文旅信息（无条件）*/
    @Operation(summary = "分页展示所有城市文旅信息")
    @PostMapping({"allCity/{pageNo}/{pageSize}","allCity/{pageNo}","allCity"})
    public ResponseEntity<Page<CityInfo>> queryAllCityByPage(  // 修改返回类型为Page<CityInfo>
                                                               @PathVariable(value = "pageNo",required = false) Integer pageNo,
                                                               @PathVariable(value = "pageSize",required = false) Integer pageSize)
    {
        if (pageNo == null) {
            pageNo = 0; // 第一页
        } else {
            pageNo = pageNo - 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        // 注意：需要同步修改Service层的queryAllCity方法返回类型为Page<CityInfo>
        return ResponseEntity.ok(this.cityInfoService.queryAllCity(pageRequest));
    }

    /*通过主键查询单条数据*/
    @GetMapping("{id}")
    public ResponseEntity<CityInfo> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.cityInfoService.queryById(id));
    }

    /*新增数据*/
    @PostMapping("add")
    @Operation(summary = "上传城市文旅信息")
    public ResponseEntity<CityInfo> add(@RequestBody CityInfo cityInfo) {
        return ResponseEntity.ok(this.cityInfoService.insert(cityInfo));
    }

    /*编辑数据*/
    @PutMapping("edit")
    @Operation(summary = "编辑城市文旅信息")
    public ResponseEntity<CityInfo> edit(@RequestBody CityInfo cityInfo) {
        return ResponseEntity.ok(this.cityInfoService.update(cityInfo));
    }

    /*删除数据*/
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.cityInfoService.deleteById(id));
    }

}